package com.mopeso.location;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;

import com.mopeso.MyApplication;


public class SystemLocation implements ILocation, LocationListener {
    private static final String WAY = "system";
    private static final long MIN_TIME = 1000;
    private static final float MIN_DISTANCE = 0.01f;


    private LocationManager locationManager;
    private ILocationCallback callback;
    private LocationProvider gpsProvider;

    public SystemLocation(Context context, ILocationCallback callback) {
        this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        this.callback = callback;
    }

    @SuppressLint("MissingPermission")
    @Override
    public void startLocation() {
        if (locationManager != null) {
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
            }
            LocationProvider netProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
            if (gpsProvider != null) {

                if (ActivityCompat.checkSelfPermission(MyApplication.getApplication(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MyApplication.getApplication(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
            }
            if (netProvider != null) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
            }
            if (gpsProvider == null && netProvider == null) {
                callback.onLocationFailed(LocationErrorType.TypeApiUnavailable, WAY,null);
            }
        }
    }

    @SuppressLint("MissingPermission")
    @Override
    public void closeLocation() {
        if (locationManager != null)
            locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        if(location!=null){
            callback.onLocationSuccess(new LocationData(location.getLatitude(),location.getLongitude(),location.getAccuracy(),WAY));
        }else{
            callback.onLocationFailed(LocationErrorType.TypeRequestLocationNull,WAY,null);
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}
